using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;  

namespace Pokemon
{
    public class BackgroundScreen : GameScreen, IAnimation
    {
        private Texture2D _pokemonLogo;
        
        private Texture2D _monsterLogo;

        private Texture2D _vsLogo;

        private TimeSpan _animationTime = TimeSpan.Zero;

        public BackgroundScreen(Game game) 
            : base(game)
        {            
        }

        public override void LoadContent()
        {
            _background = CurrGame.Content.Load<Texture2D>("Textures/Backgrounds/Background");
            _pokemonLogo = CurrGame.Content.Load<Texture2D>("Textures/Backgrounds/PokemonLogo");
            _monsterLogo = CurrGame.Content.Load<Texture2D>("Textures/Backgrounds/MonstersLogo");
            _vsLogo = CurrGame.Content.Load<Texture2D>("Textures/Backgrounds/VSLogo");
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sp = ScreenManager.SpriteBatch;
            
            sp.Begin();

            if (_isStartup)
            {
                _animationTime += gameTime.ElapsedGameTime;
                Animation(sp);
            }
            else
            {
                BackgroundAnimation(sp, 1);
                PokemonLogoAnimation(sp, 1);
                ZombieLogoAnimation(sp, 1);
                VsLogoAnimation(sp, 1);
            }
            
            foreach (var gameScreen in Screens)
            {
                gameScreen.Draw(gameTime);
            }

            sp.End();
        }

        private void VsLogoAnimation(SpriteBatch sp, float p)
        {
            Rectangle srcRect = new Rectangle(0,0, (int)(_vsLogo.Width * p), _vsLogo.Height);
            sp.Draw(_vsLogo, new Rectangle(350, 200, (int)(_vsLogo.Width * p), _vsLogo.Height), srcRect, Color.White);
        }

        private void ZombieLogoAnimation(SpriteBatch sp, float p)
        {
            var position = new Vector2(375*p, 285);
            sp.Draw(_monsterLogo, new Vector2(800 - position.X, position.Y), Color.White);
        }

        private void PokemonLogoAnimation(SpriteBatch sp, float pokemonProgress)
        {
            var position = new Vector2(200*pokemonProgress, 30);
            sp.Draw(_pokemonLogo, position, Color.White);
        }

        private void BackgroundAnimation(SpriteBatch sp, double backgroundProgress)
        {
            var width = CurrGame.GraphicsDevice.DisplayMode.Width;
            var height = CurrGame.GraphicsDevice.DisplayMode.Height;
            var rectWidth = width*backgroundProgress;
            var rectHeight = height*backgroundProgress;
            sp.Draw(_background,
                    new Rectangle((int) ((height - rectHeight)/2), (int) ((width - rectWidth)/2),
                                  (int) (rectHeight),
                                  (int) (rectWidth)), null, Color.White);
        }

        public override void Update(GameTime gameTime)
        {
            foreach (var gameScreen in Screens)
            {
                gameScreen.Update(gameTime);
            }
        }

        #region IAnimation Members

        public bool IsStartup
        {
            get { return _isStartup; }
            set { _isStartup = value; }
        }

        public TimeSpan AnimationTime
        {
            get { return _animationTime; }
            set
            {
                _animationTime = value;
            }
        }

        public void Animation(SpriteBatch sp)
        {
            var backgroundProgress = _animationTime.TotalMilliseconds / 1000;
            BackgroundAnimation(sp, backgroundProgress > 1 ? 1 : backgroundProgress);
            if (backgroundProgress < 1)
                return;
            var logoProgress = (_animationTime.TotalMilliseconds - 1000) / 2000;
            PokemonLogoAnimation(sp, logoProgress > 1 ? 1 : (float)logoProgress);
            ZombieLogoAnimation(sp, logoProgress > 1 ? 1 : (float)logoProgress);
            if (logoProgress < 1)
                return;
            var vsLogoProgress = (_animationTime.TotalMilliseconds - 3000) / 1500;
            VsLogoAnimation(sp, vsLogoProgress > 1 ? 1 : (float)vsLogoProgress);
            if (vsLogoProgress < 1)
                return;
            _isStartup = false;
            foreach (var gameScreen in _screens)
            {
                gameScreen.Startup();
            }
        }

        public override void Startup()
        {
            _animationTime = TimeSpan.Zero;
            base.Startup();
        }

        #endregion
    }
}
